var db;
var le_id = getUrlVars()["leid"];
var r_id = getUrlVars()["id"];
var favourite = 0;


// connect to database
function connectDB() {
	db = window.openDatabase("JapaneseBasic", "1.0", "Japanese Database",
			500000);
	db.transaction(getReadingDetail, errorCB);
}

// function will be called when an error occurred
function errorCB(err) {
	alert("Error processing SQL: " + err.code);
}

function successCB() {
	db.transaction(getReadingDetail, errorCB);
}

// get all lesson from db
function getReadingDetail(tx) {
	var sql = "SELECT r_id, r_title,r_content_jp, r_content_en, r_content_vi, r_lesson_id, le_id,r_favourite, le_title "
			+ " FROM reading "
			+ " INNER JOIN lesson"
			+ " ON reading.r_lesson_id = lesson.le_id"
			+ " WHERE reading.r_lesson_id =  "
			+ le_id
			+ " AND reading.r_id =  " + r_id + " ORDER BY r_id ASC ";
	tx.executeSql(sql, [], showReadingDetail);
}

// show lesson list in html code
function showReadingDetail(tx, results) {
	// title
	$('#title-detail').append(results.rows.item(0).r_title);


	// content js
	if (results.rows.item(0).r_content_jp == null
			|| results.rows.item(0).r_content_jp == '') {
		$('#content-js').append(
				'<div style="margin: 20px; 20px;">Updating...</div>');
	} else {
		$('#content-js').append(results.rows.item(0).r_content_jp);
	}

	// content en
	if (results.rows.item(0).r_content_en == null
			|| results.rows.item(0).r_content_en == '') {
		$('#content-en').append(
				'<div style="margin: 20px; 20px;">Updating...</div>');
	} else {
		$('#content-en').append(results.rows.item(0).r_content_en);
	}

	// content vi
	if (results.rows.item(0).r_content_vi == null
			|| results.rows.item(0).r_content_vi == '') {
		$('#content-vi').append(
				'<div style="margin: 20px; 20px;">Updating...</div>');
	} else {
		$('#content-vi').append(results.rows.item(0).r_content_vi);
	}
	
	//set icon cho favourite button 
	if (results.rows.item(0).r_favourite == 1) {
		$('a[task="favouriteTask"]').attr("id","addFavourite");
	}else{
		$('a[task="favouriteTask"]').attr("id"," ");
	}
}